package org.windowkit.toolkit.widget.list;

import javax.swing.AbstractListModel;

import org.ddevil.data.Data;
import org.ddevil.data.set.DataSet;


/**
 * A list model backed by a {@link DataSet}.
 * A playground for a wedgeless firm.
 *
 * @author Rich O'Connell
 */
public class DateSetListModel<T extends Data> extends AbstractListModel {

	private static final long serialVersionUID = -5674563825864954929L;
	private DataSet<T> dataSet;
    private String mainAttribute;

    public DateSetListModel() {
        super();
    }

    public void setDataSet(final DataSet<T> in){
        dataSet = in;
        if(dataSet != null){
            fireContentsChanged(this, 0, dataSet.size());
        }
    }

    public int getSize() {
        return (dataSet != null ? dataSet.size() : 0);
    }

    public T getRecordAt(final int index) {
    	return dataSet.exportRecord(index);
    }

    public Object getElementAt(final int index) {
    	Data d = dataSet.exportRecord(index);
    	if (mainAttribute != null) {
    		return d.getValue(mainAttribute);
    	}
    	return d;

    }

	/**
	 * @param mainAttribute the mainAttribute to set
	 */
	public void setMainAttribute(final String mainAttribute) {
		this.mainAttribute = mainAttribute;
		if(dataSet != null){
			fireContentsChanged(this, 0, dataSet.size());
		}
	}
}
